package com.example.demo.commons.core.util;


import com.example.demo.commons.core.enums.MoneyUnit;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * <p> 金钱转换 </p>
 *
 * @author tanyunpeng
 * @date 2021/11/18
 */
public class LocalMoneyUtils {

    public static BigDecimal fen2yuan(Long money) {
        if (money == null) {
            return BigDecimal.ZERO;
        }
        return toYuan(money, MoneyUnit.FEN, 2, RoundingMode.HALF_UP);
    }

    public static BigDecimal li2yuan(Long money) {
        if (money == null) {
            return BigDecimal.ZERO;
        }
        return toYuan(money, MoneyUnit.LI, 2, RoundingMode.HALF_UP);
    }

    public static BigDecimal toYuan(Long money, MoneyUnit unit, int scale, RoundingMode roundingMode) {
        if (money == null) {
            return BigDecimal.ZERO;
        }
        long rate = MoneyUnit.YUAN.getRate() / unit.getRate();
        return BigDecimal.valueOf(money).divide(BigDecimal.valueOf(rate), scale, roundingMode);
    }
}
